﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ross.EntityFramework;
using Ross.EntityFramework.Model;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;

namespace Ross.Service
{
    public class SignsService : BaseService, IDisposable
    {
        private TranDbContext dbContext;
        public IRepository<Signs, long> repository;
        public IRepository<Users, long> userResp;
        public IRepository<Course, long> courseResp;

        public SignsService(string nameOrConnectionString)
        {
            dbContext = new TranDbContext(nameOrConnectionString);
            repository = new Repository<Signs, long>(dbContext);
            userResp = new Repository<Users, long>(dbContext);
            courseResp = new Repository<Course, long>(dbContext);
        }
        public void Dispose()
        {
            dbContext.Dispose();
        }

        /// <summary>
        /// 报名
        /// </summary>
        /// <param name="sign"></param>
        /// <returns></returns>
        public ResultDto<bool> SignUp(Dto.Signs sign)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                var obj = repository.GetAllList(o => o.CourseID == sign.CourseID && o.UserID == sign.UserID.ToString()).FirstOrDefault();
                if (obj != null) {
                    result.status = 500;
                    result.message = "the record is exist";
                    result.datas = false;
                }
                else
                {
                    var model = Mapper.Map<Signs>(sign);
                    repository.Insert(model);
                    result.status = 200;
                    result.datas = true;
                }
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.status = 500;
                result.datas = false;
            }
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultDto<bool> Delete(long id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                repository.Delete(id);
                result.status = 200;
                result.datas = true;
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.status = 500;
                result.datas = false;
            }
            return result;
        }
        /// <summary>
        /// 根据用户获取全部报名课程
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ResultDto<List<Dto.Signs>> GetAllSignByUser(long userId = 0)
        {
            ResultDto<List<Dto.Signs>> result = new ResultDto<List<Dto.Signs>>();
            var resp = repository.GetAllList(o => o.UserID==userId.ToString());
            result.datas = Mapper.Map<List<Dto.Signs>>(resp);
            return result;
        }
        /// <summary>
        /// 获取全部报名学员
        /// </summary>
        /// <param name="teacherId"></param>
        /// <param name="courseId"></param>
        /// <returns></returns>
        public ResultDto<List<Dto.Signs>> GetAllSigner(long teacherId = -1, long courseId = -1)
        {
            ResultDto<List<Dto.Signs>> result = new ResultDto<List<Dto.Signs>>();
            List<Signs> resp = new List<Signs>();
            if (teacherId > 0)
            {
                var courses = courseResp.GetAllList(o => o.UserID == teacherId).Select(o => o.ID);
                resp = repository.GetAllList(o => courses.Contains(o.CourseID));
            }
            if(courseId>0)
            {
                resp = repository.GetAllList(o => o.CourseID == courseId);
            }
            result.datas = Mapper.Map<List<Dto.Signs>>(resp);
            return result;
        }
        /// <summary>
        /// 学生积分，学习时长统计，报名课程结业
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultDto<bool> SignOver(long id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                var obj = repository.Get(id);
                if (obj != null && !obj.IsOver)
                {
                    obj.IsOver = true;
                    repository.Update(obj);
                    var user = userResp.Get(long.Parse(obj.UserID));
                    if (user != null)
                    {
                        var course = courseResp.Get(obj.CourseID);
                        if (course != null)
                        {
                            user.StudyTimes += course.Times;
                            user.Points += course.Points;
                            userResp.Update(user);
                        }
                    }
                    result.status = 200;
                    result.datas = true;
                }
                else
                {
                    result.status = 500;
                    result.datas = false;
                    result.message = "record is not exist or has been sign over.";
                }
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.status = 500;
                result.datas = false;
            }
            return result;
        }
    }
}
